// index.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
const app = getApp();
import {
  saveUser
} from '../../api/index.js'
Page({

  data: {
    motto: 'Hello World',
    userInfo: {
      avatarUrl: defaultAvatarUrl,
      nickName: '',
      phone: null,
      code:'',
      isShow:false
    },
    hasUserInfo: true, // 判断是否信息都要填写
    isLoad: false, // 判断是否发送登录请求
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
  },

  // 获取头像
  onChooseAvatar(e) {
    const {
      avatarUrl
    } = e.detail
    const {
      nickName
    } = this.data.userInfo
    const {
      phone
    } = this.data.userInfo

    if (nickName && avatarUrl && avatarUrl !== defaultAvatarUrl && phone) {
      this.setData({
        "hasUserInfo": true,
      })
    }

    wx.getFileSystemManager().readFile({
      filePath: e.detail.avatarUrl, // 临时文件路径
      encoding: 'base64',
      success: res => {

        // 在这里你可以使用 res.data，它包含了 base64 编码的文件内容
        // 例如，将 base64 数据用于你的业务逻辑
        this.setData({
          "userInfo.avatarUrl": 'data:image/jpeg;base64,' + res.data
        })
      },
      fail: err => {
        console.error('读取文件失败', err);
      }
    });
    console.log(this.data.userInfo);
  },
  // 获取昵称
  onInputChange(e) {
    const nickName = e.detail.value
    const {
      avatarUrl
    } = this.data.userInfo
    const {
      phone
    } = this.data.userInfo
    this.setData({
      "userInfo.nickName": nickName,
    })
    if (nickName && avatarUrl && avatarUrl !== defaultAvatarUrl && phone !== null) {
      this.setData({
        "hasUserInfo": true,
      })
    }
    console.log(this.data.userInfo);
    console.log(this.data.hasUserInfo);
  },
  // 输入手机号
  phoneChange(e) {
    console.log(e);
    let value = e.detail.value
    const valuephone = Number(value)
    this.setData({
      "userInfo.phone": valuephone,
    })

    console.log(this.data.hasUserInfo);
  },
  // 输入手机号
  codeChange(e) {
    console.log(e);
    let value = e.detail.value
    const valuecode = value
    this.setData({
      "userInfo.code": valuecode,
    })

    console.log(this.data.hasUserInfo);
  },
  // 登录
  async login() {
    if (this.data.userInfo.avatarUrl === defaultAvatarUrl) {
      return wx.showToast({
        title: '请选择头像',
        icon: 'none'
      });
    }
    if (!this.data.userInfo.nickName) {
      return wx.showToast({
        title: '请输入昵称',
        icon: 'none'
      });
    }
    const phoneRegex = /^1[3-9]\d{9}$/;
    if (!phoneRegex.test(this.data.userInfo.phone)) {
      return wx.showToast({
        title: '请输入有效的手机号码',
        icon: 'none'
      });
    }
    if (!this.data.userInfo.code || this.data.userInfo.code !== "rzhtech") {
      console.log(this.data.userInfo);
      return wx.showToast({
        title: '请输入正确的授权码',
        icon: 'none'
      });
    }
    console.log(this.data.userInfo);
    wx.showLoading({
      title: '登录中...',
    })
    this.setData({
      "hasUserInfo": false,
    })
    wx.setStorage({
      key: "userInfo",
      data: this.data.userInfo
    })
    const data = this.data.userInfo
    const res = await saveUser(data)
    console.log(res, 'saveUser');
    if (res.data.code === 200) {
      setTimeout(() => {
        wx.hideLoading()
        if (this.data.userInfo.phone=== 15312344321 && this.data.userInfo.nickName === 'admin') {
          wx.redirectTo({
            url: "/pages/my/my",
          })
        } else {
          wx.redirectTo({
            url: "/pages/show/show",
          })
        }
      }, 2000)
    } else {
      wx.hideLoading()
      wx.showToast({
        title: res.data.msg,
        icon: 'none',
        duration: 2500
      });
    }
  },
  onLoad(){
    app.bgPause()
  },
  onUnload(){
    app.bgPlay()
  },

})